package com.edocbase.desktop.plugins.mdiary;

import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

import com.edocbase.desktop.lightweight.widgets.BreadcrumbBar;
import com.edocbase.desktop.lightweight.widgets.BreadcrumbItem;
import com.edocbase.desktop.lightweight.widgets.ControlFigure;
import com.edocbase.desktop.lightweight.widgets.swt.ComboSelector;

public class URLBreadcrumbItem extends BreadcrumbItem {
	private ComboSelector addressCombo = null;
	private MDiaryItemCreateDelegate actionDelegator = null;
	public URLBreadcrumbItem(BreadcrumbBar bar,MDiaryItemCreateDelegate delegate) {
		super(bar);
		this.actionDelegator = delegate;
	}

	@Override
	protected SplitButtonPart createPart(boolean top) {
		if (top){
			
			return new URLButtonPart();
		}
		return super.createPart(top);
	}

	
	private class URLButtonPart extends SplitButtonPart{
		private ControlFigure<ComboSelector> inputFigure = null;
		public URLButtonPart() {
			super();
			
		}

		@Override
		protected void paintClientArea(Graphics g) {
			super.paintClientArea(g);
			if (inputFigure == null){
				Control c = bar.getOwner();
				addressCombo = new ComboSelector((Composite)c, SWT.BORDER);
				addressCombo.addKeyListener(new KeyListener() {
					
					@Override
					public void keyReleased(KeyEvent e) {
						if (e.keyCode == 13){
							String text = addressCombo.getText();
							text = text.trim();
							if (text.length() > 0){
								actionDelegator.gotoURL(text);
							}
						}
					}
					
					@Override
					public void keyPressed(KeyEvent e) {
						
					}
				});
				inputFigure = new ControlFigure<ComboSelector>(addressCombo);
				setContents(inputFigure);
				if (getText() != null){
					addressCombo.setText(getText());
				}
			}
		}

		@Override
		public Dimension getPreferredSize(int wHint, int hHint) {
			Dimension d = super.getPreferredSize(wHint, hHint);
			d.height -= 2;
			int w =200;
			int width = d.width + w;
			if (width > 400){
				width = 400;
			}
			d.width =width;
			return d;
		}

		@Override
		protected void paintButton(Graphics g) {
//			Rectangle rect = this.getClientArea();
//			int x = rect.x;
//			int y = rect.y;
//			int w = rect.width;
//			int h = rect.height;
//			if (this.text != null && this.text.length() > 0) {
//				Dimension d = TextUtilities.INSTANCE.getStringExtents(
//						this.text, getFont());
//				y = rect.y + (h - d.height) / 2;
//				g.drawText(this.text, x, y);
//			}
		}

		@Override
		public void dispose() {
			super.dispose();
			if (inputFigure != null){
				inputFigure.dispose();
				inputFigure = null;
			}
			if (addressCombo != null){
				if (!addressCombo.isDisposed()){
					addressCombo.dispose();
				}
				addressCombo = null;
			}
		}
		
	}
}
